/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2019/1/19
 * Time: 23:41
 */
$(function () {

    var ue = UE.getEditor('editor', {initialFrameHeight: 250});

    sendAjax('/v1/manager/grow/rule/detail.json', 'get', {id: $('.grow-id').val()}, function (json) {
        var data = json.body;
        $('.grow-title').val(data.title);
        $('.grow-type').val(data.type);
        $('.grow-type option').each(function () {
            if ($(this).val() == data.type) {
                $(this).attr('selected', 'selected');
            }
        });
        $('.grow-content').val(data.content);
        ue.setContent(data.content);
    });

    $('.btn-save').on('click', function(){
        $('.grow-content').val(ue.getContent());
        var data = {
            id: $('.grow-id').val(),
            title: $('.grow-title').val(),
            type: $('.grow-type').val(),
            content: $('.grow-content').val()
        };

        if (checkForm(data)) {
            saveData('/v1/manager/save/grow/rule', {params:data}, '/manager/grow.html');
        }
    });
});

function checkForm(data) {
    if (data.title == '') {
        alertTip('标题不能为空');
        return false;
    }
    if (data.type == '') {
        alertTip('请选择类型');
        return false;
    }
    if (data.content == '') {
        alertTip('内容不能为空');
        return false;
    }
    return true;
}